<template>
    <div>
        <el-dialog type="primary" :visible.sync="dialoge5" width="80%" append-to-body @close="closeDialog">
            <el-form ref="form" :model="form" inline :rules="rules">
                <el-row>
                    <el-form-item prop="fTaskName" label="任务名称">
                        <el-input v-model="form.fTaskName" placeholder="请输入任务名称" style="width: 200px" />
                    </el-form-item>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="VS值来源">
                            <el-input v-model="form.fVsSource" placeholder="请选择VS值来源" style="width: 200px"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="6">
                        <el-form-item label="使用VS值类型">
                            <el-checkbox-group v-model="fWaveOutcomeList" @change="changeWave(fWaveOutcomeList)">
                                <el-checkbox v-for="item in fWaveOutcome" :label="item" :key="item">{{ item
                                    }}</el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="温度段(最小)">
                            <el-input v-model="form.fMinHeatStage" placeholder="请选择温度段(最小)"
                                style="width: 150px"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="6">
                        <el-form-item label="温度段(最大)">
                            <el-input v-model="form.fMinIsExecute" placeholder="请选择温度段(最大)"
                                style="width: 150px"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="温箱静置时间(分钟)">
                            <el-input v-model="form.fQuietMinute" placeholder="请选择温箱静置时间(分钟)"
                                style="width: 150px"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="6">
                        <el-form-item label="测试次数">
                            <el-input v-model="form.fTextNum" placeholder="请选择测试次数" style="width: 150px"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <div class="grid-content bg-purple">
                            <el-form-item prop="productName" label="选择期待LAB值">
                                <div @click="openLab()" class="el-input-div" v-if="form.fIsPlan == 1">{{
                                        form.bcmClientPreset ? form.bcmClientPreset.fClientPresetName : '请选择期待Lab值方案'
                                    }}</div>
                                <el-checkbox label="是否手动填写" v-model="form.fIsPlan" :true-label="1" :false-label="0"
                                    @change="changePlan($event)" style="margin-left: 6px;"
                                    :disabled="form.check == 1"></el-checkbox>
                            </el-form-item>
                        </div>
                        <div class="grid-content bg-purple-light" v-if="form.fIsPlan == 0">
                            <el-table :data="form.bcmClientPresetDetailList">
                                <el-table-column prop="color" align="center" label="颜色">
                                    <template slot-scope="scope">
                                        <span>{{ colorDetail(scope.row.fColor) }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column label="最小 L 值" prop="fLMin" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fLMin" type="number" placeholder="请输入最小 L 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最大 L 值" prop="fLMax" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fLMax" type="number" placeholder="请输入最大 L 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最小 A 值" prop="fAMin" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fAMin" type="number" placeholder="请输入最小 A 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最大 A 值" prop="fAMax" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fAMax" type="number" placeholder="请输入最大 A 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最小 B 值" prop="fBMin" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fBMin" type="number" placeholder="请输入最小 B 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最大 B 值" prop="fBMax" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fBMax" type="number" placeholder="请输入最大 B 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                            </el-table>
                        </div>
                        <div class="grid-content bg-purple-light" v-if="form.fIsPlan == 1">
                            <el-table :data="form.bcmClientPreset">
                                <el-table-column prop="color" align="center" label="颜色">
                                    <template slot-scope="scope">
                                        <span>{{ colorDetail(scope.row.fColor) }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column label="最小 L 值" prop="fLMin" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fLMin" type="number" placeholder="请输入最小 L 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最大 L 值" prop="fLMax" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fLMax" type="number" placeholder="请输入最大 L 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最小 A 值" prop="fAMin" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fAMin" type="number" placeholder="请输入最小 A 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最大 A 值" prop="fAMax" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fAMax" type="number" placeholder="请输入最大 A 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最小 B 值" prop="fBMin" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fBMin" type="number" placeholder="请输入最小 B 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                                <el-table-column label="最大 B 值" prop="fBMax" align="center">
                                    <template slot-scope="scope">
                                        <el-input v-model.number="scope.row.fBMax" type="number" placeholder="请输入最大 B 值"
                                            clearable style="width: 100px" :disabled="isEdit == 1" />
                                    </template>
                                </el-table-column>
                            </el-table>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="刷新图片" width="200" align="center">
                            <el-button v-if="!form.fPictureUrl" @click="uploadFile" size="mini"
                                type="primary">选取文件</el-button>
                            <div v-else>
                                <el-link @click="uploadFile" :underline="false" target="_blank">
                                    <span class="el-icon-document"> {{ form.wfTaskTestDetail.fPictureUrl ? getFileName(form.wfTaskTestDetail.fPictureUrl)
                                        :
                                        '' }} </span>
                                </el-link>
                                <a style="color: #2561EF;" target="_blank" :download="getFileName(form.wfTaskTestDetail.fPictureUrl)"
                                    :href="form.fPictureUrl" :name="getFileName(form.wfTaskTestDetail.fPictureUrl)">下载</a>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <div class="grid-content bg-purple">
                            <el-form-item prop="fDriverWaitTime" label="驱动板等待时间（秒）">
                                <div @click="openWait()" class="el-input-div">{{
                                    form.wfTaskTestDetail.wfDriverWait ? form.wfTaskTestDetail.wfDriverWait.fDriverWaitTime : '请选择驱动板等待时间' }}
                                </div>
                            </el-form-item>
                        </div>
                    </el-col>
                </el-row><br>
                <div class="flex">
                    <div class="w-20" style="font-size: 16px;font-weight: 200;">任务流程</div>
                    <div
                        style="vertical-align: middle;text-align: right;display: inline-block;width: 80%;border-top: 1px solid #ccc;margin-top:10px">
                    </div>
                </div><br>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialoge5 = false">取 消</el-button>
                <el-button type="primary" @click="toSure">确 定</el-button>
            </div>
        </el-dialog>
        <Setting ref="setting" @sure="getSetting"></Setting>
        <el-dialog :visible.sync="showUploadImg" title="选择文件" width="70%">
            <uploadImg @close="showUploadImg = false" @sure="getImg"></uploadImg>
        </el-dialog>
        <Setting ref="setting" @sure="getSetting"></Setting>
        <Lab ref="lab" @sure="getLab"></Lab>
        <Wait ref="wait" @sure="getWait"></Wait>
        <el-dialog title="选择波形" :visible.sync="dialogWaveVisibles" append-to-body width="80%">
            <addWave @closeWave="closeWave" @confirmWave="confirmWave"></addWave>
        </el-dialog>
    </div>
</template>

<script>
import Setting from './setting.vue'
import Lab from './lab.vue'
import Wait from './wait.vue'
import { taskLst } from '@/api/workOrder/administration'
import addWave from '@/views/basicData/product/components/addWave.vue';
export default {
    name: 'task',
    components: {
        Setting,
        Lab,
        Wait,
        addWave,
    },
    data() {
        return {
            fWaveOutcome: ["VS_W", "VS_R",],
            fWaveOutcomeList: [],
            resource: '',
            dialoge5: false,
            dialogVisible: false,
            dialogWaveVisibles: false,
            form: {
                fTaskName: '',
                fVsSource: '',
                fPictureUrl: null,
                fIsMes: false,
                wfOrderTaskEntryList: [],
                wfTaskTestDetail:[],
                fTaskType: 1,
                bcmClientPreset: null,
                check: 0,
                fIsPlan: 0
            },
            queryParams: {
                fTaskName: '',
                fVsSource: '',
                fIsMes: false,
                fHeatStage: '',
                fIntervalVoltage: '',
                fMaxVoltage: '',
                fMinVoltage: '',
                fClientPresetId: '',
                fVsValue: '',
                fVcomValue: '',
                fUrlState: '',
                fPictureUrl: '',
                fTaskType: 0,
                fQuietMinute: '',
                fDriverWai: '',
                fTextNum: '',
                fIsSetting: '0',
                fTestName: '',
                wfTaskTestEntry: null,
                bcmClientPreset: {
                    fClientPresetName: ''
                }
            },
            settingKey: -1,
            labKey: -1,
            waitKey: -1,
            postList: [],
            rules: {
                fTaskName: [{ required: true, message: '请输入任务名称', type: 'string' }]
            },
            imgIndex: 0,
            showUploadImg: false,
            waveIndex: 0
        }
    },
    methods: {
        changePlan(event) {
            // 当复选框被选中时，设置div为禁用状态
            this.isCheckboxChecked = event.target;
        },
        changeWave(e) {
            console.log(e)
            this.form.fWaveOutcome = e
        },
        colorDetail(id) {
            return this.dict.type.wf_module_color.find(item => item.value == id).label
        },
        openLab() {
            if (this.form.check == 1) return
            this.$refs.lab.where.fProductId = this.form.fProductId
            this.$refs.lab.open()
        },
        handleInput(row) {
            const inputValue = row.fMaxVoltage;
            if (this.form.fTaskType == 1 && inputValue <= 0) {
                this.fMaxVoltage = ''
                row.fMaxVoltage = null;
            }
            if (this.form.fTaskType == 2 && inputValue >= 0) {
                this.fMaxVoltage = ''
                row.fMaxVoltage = null;
            }
        },
        handleInputs(row) {
            const inputValue = row.fMinVoltage;
            if (this.form.fTaskType == 1 && inputValue <= 0) {
                this.fMinVoltage = ''
                row.fMinVoltage = null;
            }
            if (this.form.fTaskType == 2 && inputValue >= 0) {
                this.fMinVoltage = ''
                row.fMinVoltage = null;
            }
        },
        getImg(e) {
            console.log(e)
            this.showUploadImg = false
            this.form.wfTaskTestDetail.fPictureUrl = e.fPictureUrl
            this.form.wfTaskTestDetail.fPictureId = e.fId
            this.form.wfTaskTestDetail.fUrlState = this.form.wfTaskTestDetail.fPictureUrl ? 1 : 0
            console.log(e.fPictureUrl,'55555');
        },
        getFileName(name) {
            // 如果是 URL 那么取最后的名字，如果不是直接返回
            if (name && name.lastIndexOf("/") > -1) {
                return name.slice(name.lastIndexOf("/") + 1);
            } else {
                return name || '';
            }
        },
        open() {
            this.dialoge5 = true
            if (this.form.wfTaskTestDetail.length === 0) {
                this.form.wfTaskTestDetail.push({ ...this.queryParams })
            }
        },
        uploadFile() {
            this.showUploadImg = true
        },
        handleUpdate() {
            this.form.wfOrderTaskEntryList.push({ ...this.queryParams })
        },
        handleCopy(row) {
            // let arr = row
            this.form.wfOrderTaskEntryList.push({ ...row })
        },
        handleDelete(index) {
            this.form.wfOrderTaskEntryList.splice(index, 1)
        },
        getLab(e) {
            this.form.wfTaskTestDetail.bcmClientPreset.fClientPresetId = e.fId
            this.form.wfTaskTestDetail.bcmClientPreset.fClientPresetName = e.fClientPresetName
            this.$set(this.form.wfTaskTestDetail, this.labKey, { ...this.form.wfTaskTestDetail, fClientPresetId: e.fId });
            this.$set(this.form.wfTaskTestDetail, this.labKey, { ...this.form.wfTaskTestDetail, fClientPresetName: e.fClientPresetName });
            this.$set(this.form.wfTaskTestDetail, this.labKey, { ...this.form.wfTaskTestDetail, fClientPresetName: e.wfTaskPresetDetailList            });
        },
        getWait(e) {
            this.form.wfTaskTestDetail.wfDriverWait = {}
            this.form.wfTaskTestDetail.wfDriverWait.fDriverWaitTime = e.fDriverWaitTime
            this.$set(this.form.wfTaskTestDetail, this.waitKey, { ...this.form.wfTaskTestDetail, fDriverWaitName: e.fDriverWaitName });
            this.$set(this.form.wfTaskTestDetail, this.waitKey, { ...this.form.wfTaskTestDetail, fDriverWait: e.fId });
            this.$set(this.form.wfTaskTestDetail, this.waitKey, { ...this.form.wfTaskTestDetail, fDriverWaitTime: e.fDriverWaitTime });
        },
        confirmWave(e) {
            this.dialogWaveVisibles = false;
            this.$set(this.form.wfOrderTaskEntryList, this.waveIndex, { ...this.form.wfOrderTaskEntryList[this.waveIndex], fWaveId: e.fId });
            this.$set(this.form.wfOrderTaskEntryList, this.waveIndex, { ...this.form.wfOrderTaskEntryList[this.waveIndex], fWaveName: e.fWaveName });
            this.form.fWaveId = e.fId
            this.form.fWaveName = e.fWaveName
        },
        closeWave() {
            this.dialogWaveVisibles = false;
        },
        openWave(index) {
            this.waveIndex = index
            this.dialogWaveVisibles = true
        },
        openWait(index) {
            if (this.form.check == 1) return
            if (index) {
                this.waitIndex = index
            }
            this.$refs.wait.where.fProductId = this.form.fProductId
            this.$refs.wait.open()
        },
        toSure() {
            if (!this.form.fTaskName) {
                this.$message({
                    message: '请输入任务名称',
                    type: 'warning'
                })
                return
            }
            if (this.form.wfOrderTaskEntryList.length < 1) {
                this.$message({
                    message: '请添加任务',
                    type: 'warning'
                })
                return
            }
            if (this.form.wfOrderTaskEntryList.some(item => !item.fHeatStage)) {
                this.$message({
                    message: '请输入温度段',
                    type: 'warning'
                })
                return
            }
            if (this.form.wfTaskTestDetail.some(item => !item.fPictureUrl)) {
                this.$message({
                    message: '请上传图片',
                    type: 'warning'
                })
                return
            }
            if (this.form.wfOrderTaskEntryList.some(item => item.fIsSetting == 0)) {
                if (this.form.wfOrderTaskEntryList.some(item => !item.fDriverWaitTime)) {
                    this.$message({
                        message: '请选择驱动板等待时间',
                        type: 'warning'
                    })
                    return
                }
            }
            this.form.fWaveName = '';
            this.form.wfOrderTaskEntryList = [...this.form.wfOrderTaskEntryList]
            this.$emit("sure", { ...this.form })
            this.form.fIsMes = this.form.fIsMes ? 1 : 0
            this.dialoge5 = false
        },
        closeDialog() {
            this.form.fTaskName = ""
            this.form.wfOrderTaskEntryList.forEach(e => {
                e.bcmClientPreset.fClientPresetName = ''
            });
            this.form.wfOrderTaskEntryList = []
        },
        getSetting(e) {
            console.log("e", e);
            this.form.wfOrderTaskEntryList[this.settingKey].wfTaskTestEntry = e
            this.form.wfOrderTaskEntryList[this.settingKey].fTestName = e.fTestProject
        }
    }
}
</script>

<style lang="scss" scoped>
.el-input-div {
    width: 180px;
    height: 36px;
    box-sizing: border-box;
    padding: 0 15px;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    line-height: 36px;
    cursor: pointer;
}
</style>